package com.sunday.volunteer.admin.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.Member;
import com.sunday.volunteer.admin.event.LoginEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;
import com.sunday.volunteer.admin.ui.index.MainDrawerActivity;
import com.sunday.volunteer.admin.utils.StatusBarUtil;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 登录
* */
public class LoginActivity extends BaseActivity {

    @Bind(R.id.user_name)
    ClearEditText userName;
    @Bind(R.id.password)
    ClearEditText password;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.checkbox)
    TextView checkbox;
    @Bind(R.id.tv_forgetPwd)
    TextView tvForgetPwd;
    @Bind(R.id.tv_crateGroup)
    TextView tvCrateGroup;

    private String phone;
    private String pwd;
    private boolean loginSelf;
    private static int APP = 1;//用户版
    private static int ADMIN = 2;//管理版
    private static long SEVEN_DAYS = 7*86400000;
    private Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        StatusBarUtil.setTransparentForImageView(this, null);
        long oldLoginTime = SharePerferenceUtils.getIns(mContext).getLong("login_time",0);
        if (SharePerferenceUtils.getIns(mContext).getBoolean("is_loginSelf",false)){
            if ((Calendar.getInstance().getTimeInMillis() - oldLoginTime)<SEVEN_DAYS){
                if (SharePerferenceUtils.getIns(mContext).getOAuth()!=null){
                    Intent intent = new Intent(mContext, MainDrawerActivity.class);
                    startActivity(intent);
                }
            }
        }
    }

    @OnClick({R.id.btn_login, R.id.tv_forgetPwd, R.id.tv_crateGroup,R.id.checkbox})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                phone = userName.getText().toString().trim();
                pwd = password.getText().toString().trim();
                if(TextUtils.isEmpty(phone)){
                    ToastUtils.showToast(mContext,"手机号不能为空");
                    return;
                }else if(!StringUtils.isMobileNO(phone)){
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                    return;
                }else if(TextUtils.isEmpty(pwd)){
                    ToastUtils.showToast(mContext,"密码不能为空");
                    return;
                }
                //登录
                login();
                break;
            case R.id.tv_forgetPwd:
                Intent pwd = new Intent(mContext,ForgetPwdActivity.class);
                startActivity(pwd);
                break;
            case R.id.tv_crateGroup:
                Intent intent = new Intent(mContext,CrateGroupActivity.class);
                startActivity(intent);
                break;
            case R.id.checkbox:
                checkbox.setSelected(!checkbox.isSelected());
                break;
        }
    }

    //login
    private void login(){
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = VolunteerClient.getVolunteerAdapter().login(phone,pwd,ADMIN);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    member = response.body().getResult();
                    if (checkbox.isSelected()){
                        SharePerferenceUtils.getIns(mContext).putBoolean("is_loginSelf", true);
                        SharePerferenceUtils.getIns(mContext).putLong("login_time", Calendar.getInstance().getTimeInMillis());
                    }
                    SharePerferenceUtils.getIns(mContext).putBoolean("is_first", false);
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                    SharePerferenceUtils.getIns(mContext).putInt(Constants.LOGIN_TYPE, 1);
                    SharePerferenceUtils.getIns(mContext).putLong(Constants.MEMBERID, member.getId());
                    Intent intent = new Intent(mContext, MainDrawerActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    public void onEvent(LoginEvent event){

    }
}
